﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PADWH35
{
    public class DBHelper
    {
        private static string conStr = "Data Source=192.168.100.250;Initial Catalog=EPICOR10TEST;User ID=szmis;pwd=szmis";
        private static SqlConnection con = null;
        /// <summary>执行SQL语句并返回数据表
        /// </summary>
        /// <param name="Sqlstr">SQL语句</param>
        /// <returns></returns>
        public static DataTable ExecuteDt(String Sqlstr, SqlParameter[] param = null, int Timeout = 30 )
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandTimeout = Timeout;
                    cmd.CommandText = Sqlstr;
                    if (param != null) cmd.Parameters.AddRange(param);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    conn.Open();
                    da.Fill(dt);
                    conn.Close();
                    return dt;
                }
            }
            catch(Exception ex)
            {
                WriteLog.Writelog(ex.ToString());
                return null;
            }
        }

        /// <summary> 执行SQL语句
        /// </summary>
        /// <param name="Sqlstr">SQL语句</param>
        /// <param name="param">参数对象数组</param>
        /// <returns></returns>
        public static int ExecuteSql(String Sqlstr, SqlParameter[] param = null, int Timeout = 30)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandTimeout = Timeout;
                cmd.Connection = conn;
                cmd.CommandText = Sqlstr;
                if (param != null) cmd.Parameters.AddRange(param);
                conn.Open();
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                return i;
            }
        }
    }
}
